package com.qf.java2007.service.impl;


import com.qf.java2007.mapper.AdminMapper;
import com.qf.java2007.pojo.Admin;
import com.qf.java2007.pojo.Reader;
import com.qf.java2007.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author AzureSky_X
 * @Date 2021/3/9 15:52
 * @Version 1.0
 */
@Service
@Transactional
public class AdminServiceImpl  implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    /**
     * 登录验证
     * @param adminName 用户输入的管理员名
     * @param password  用户输入的密码
     * @return
     */
    @Override
    public Admin login(String adminName, String password) {
        //根据用户输入的管理员名查询是否有此管理员信息
        Admin admin=adminMapper.findByName(adminName);

        if(admin != null) {
            //校验密码
            if (admin.getPassword().equals(password)) {
                return admin;
            } else {
                throw new RuntimeException("账号或密码错误！");
            }
        } else {
            throw new RuntimeException("账户不存在，请联系管理员！");
        }

    }

    /**
     * 插入读者
     * @param
     */
    @Override
    public void addRreaderInfo(Reader reader) {

        adminMapper.addRreaderInfo(reader);
        reader.setReaderName("r"+reader.getReaderId());
        adminMapper.addRreaderCard(reader);

    }
    //
    //@Override
    //public void addReader(Object reader) {
    //
    //    //adminMapper.addRreaderCard(readerCard);
    //    //adminMapper.addRreaderInfo(readerInfo);
    //}
    //@Override
    //public void addReader(Object reader) {
    //
    //    adminMapper.addRreaderCard(reader);
    //    adminMapper.addRreaderInfo(reader);
    //}

    /**
     * 更新密码
     * @param
     */
    @Override
    public void updatePwd(String reNewPasswd,Long adminid) {
        adminMapper.updatePwd(reNewPasswd, adminid);

    }

    @Override
    public void deleteReader(Long readerId) {
        adminMapper.deleteReaderCard(readerId);
        adminMapper.deleteReaderInfo(readerId);
    }


}
